#include <stdio.h>
#include<fstream>
#include<sys/types.h>
#include <iostream>
#include "light.h"
#include "fire.h"
#include "infr.h"
#include "th.h"
#include "get_time.h"
#include "insert.h"
#include "play_audio.h"
using namespace std;
//const string fire_alarm="mplayer audio/fire_alarm.mp3";
void *get_th(void *arg);
int main()
{
 if(wiringPiSetup()==-1)
 	return 1;
 bool ilstate=0;
 bool lv=0;
 bool fv=0;
 bool iv=0;
 tem_hu thv;
 string st=get_time();

 pthread_t tid1;//the temperature and humidity thread
 tmysql idata;
 pid_t fpid,lpid,thpid,ipid;
 fpid=fork();
 if(fpid<0)
	 exit(0);
 else if(fpid==0)
 {
	while(true){
		fire(fv);
		if(fv==0)
		{				
		  ofstream fiout("/home/pi/db/fire",ios::out|ios::app);
		  fiout<<fv<<"\t"<<get_time()<<endl;
		  fiout.close();
		  idata.fire=fv;
		  system("mplayer /home/pi/autorun/fire_alarm.mp3 </dev/null >/dev/null 2>1&");
		}
		sleep(3);
	}
 }
 else
 {
	lpid=fork();
	if(lpid<0)
		exit(0);
	else if(lpid==0)
	{
		while(true){
			light(lv);
			if(lv==0)
			{
			  //idata.fire=fv; up
				ofstream lout("/home/pi/db/light",ios::out|ios::app);
		  		lout<<lv<<"\t"<<get_time()<<endl;
		  		lout.close();
			}
			sleep(30);
		}
	}
	else
	{
		ipid=fork();
		if(ipid<0)
			exit(0);
		else if(ipid==0)
		{
			while(true){
				infr(iv);
				//cout<<iv<<"\t"<<ilstate<<endl;
				if(iv==1&&ilstate==0)
				{
				  for(int i=0;i<3;i++)
				  {
					system("/home/pi/autorun/shoot.sh");
					sleep(0.5);
				  }
				  ofstream isout("/home/pi/db/infr",ios::out|ios::app);
		  		  isout<<iv<<"\t"<<get_time()<<endl;
		  		  isout.close();
				  ilstate=iv;
				  sleep(1);
				}
				else if(iv==1&&ilstate==1)
				{
					system("/home/pi/autorun/shoot.sh");
					ofstream iout("/home/pi/db/infr",ios::out|ios::app);
		  			iout<<iv<<"\t"<<get_time()<<endl;
		  			iout.close();
					ilstate=iv;
					sleep(3);
				}
				else 
				{
					sleep(1);
					ilstate=iv;
				}
			}
		}
		else
		{
			thpid=fork();
			if(thpid<0)
				exit(0);
			else if (thpid==0)
			{
					while(true){
							get_th(&thv);
							ofstream thout("/home/pi/db/th",ios::out|ios::app);
							thout<<thv.tem<<"\t"<<thv.hum<<"\t"<<get_time()<<endl;
							thout.close();
							sleep(30);
					}
			}
			else
			{
				while(true){
				sleep(10000);
				}
			}
		}
	}
 }
}
 /*
 while(1){
	 pthread_create(&tid1,NULL,get_th,&thv);
	 fire(fv);
	 idata.fire=fv;
	 if(fv==0)
	 {
	  idata.fire=fv;
	  system("mplayer /home/pi/autorun/fire_alarm.mp3");
	 }
	 light(lv);
	 sleep(3);
	 if(pthread_cancel(tid1)==0)
	 	;
	  //cout<<"thread cancelled"<<endl;
	 if(thv.tem>49||thv.tem<1||thv.hum>90||thv.hum<1)
	  pthread_create(&tid1,NULL,get_th,&thv);
	 sleep(5);
	   //cout<<"thread cancelled again"<<endl;
	 if(pthread_cancel(tid1)==0)
	 	;
	 infr(iv);
	 st=get_time();
	 idata.tem=thv.tem;
	 idata.humi=thv.hum;
	// cout<<thv.tem<<"\t";
	 //cout<<"get temperature ok:"<<thv.tem<<"\t"<<thv.hum<<endl;
	 idata.light=lv;
	 idata.infr=iv;
	 idata.stime=st;
	 //cout<<"begain photo\t"<<"infr:"<<idata.infr<<"\tilstate:"<<ilstate<<endl;
	 if(idata.infr==1&&ilstate==0)
	 {
	  for(int i=0;i<3;i++)
	  {
	    system("/home/pi/autorun/shoot.sh");
	    sleep(0.3);
	  }
	 }
	 else if(idata.infr==1&&ilstate==1)
	 {
	   system("/home/pi/autorun/shoot.sh");
	   fire(fv);
	   if(fv==0)
	   { 
	    idata.fire=fv;
	    system("mplayer /home/pi/autorun/fire_alarm.mp3");
	   }
	   sleep(2);
	 }
	 else if(idata.infr==0&&ilstate==0)
	   sleep(3);
	 //cout<<"photo ok"<<endl;
	 //cout<<"insert ok"<<endl;
	 //cout<<idata.infr<<"\t"<<ilstate<<endl;
	// cout<<thv.tem<<endl;
	 for(int i=0;i<5;i++)
	 {
	  fire(fv);
	  if(fv==0)
	  { 
	    idata.fire=fv;
	    system("mplayer /home/pi/autorun/fire_alarm.mp3");
	  }
	  sleep(1);
         }
	 ins_data(idata);
	 ilstate=iv;
	 thv.tem=0;
	 thv.hum=0;
 }
 */
